from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from .models import GoodsInfo,Classification,Commodity_specification,Store
# Create your views here.
import json

def goodsHomePages(request):
    if request.method == "GET":
        data = {}
        banner = Store.objects.select_related()[0]
        banner1 = {"id":banner.home_page_banner.GoodsInfo_ID,"url":banner.home_page_banner.pic_url}
        banner2 = {"id":banner.home_page_banner2.GoodsInfo_ID,"url":banner.home_page_banner2.pic_url}
        banner3 = {"id":banner.home_page_banner3.GoodsInfo_ID,"url":banner.home_page_banner3.pic_url}
        banner4 = {"id":banner.home_page_banner4.GoodsInfo_ID,"url":banner.home_page_banner4.pic_url}
        bannerLst = [banner1,banner2,banner3,banner4]

        goodsCount = GoodsInfo.objects.count()
        goodsList = []

        for i in range(1,6):
            res = GoodsInfo.objects.select_related().filter(category_ID=i)
            itemList = []
            for j in res:
                dict = {"id":j.GoodsInfo_ID,"name":j.name,"goods_number":j.goods_number,"retail_price":j.retail_price,"pic_url":j.pic_url}
                itemList.append(dict)
            goodsList.append(itemList)
        data["goodsCount"] = goodsCount
        data["banner"] = bannerLst
        data["goodsList"] = goodsList
        return JsonResponse(data=data,safe=False)

def detail(request):

    if request.method == "GET":
        id = request.GET.get("id")
        goods = GoodsInfo.objects.filter(GoodsInfo_ID=id)[0]
        galleryList = json.loads(goods.gallery)

        specificationList = json.loads(goods.specification)
        goodsDetail = json.loads(goods.goods_desc)

        goodstotal = {"id":goods.GoodsInfo_ID,"name":goods.name,"goods_brief":goods.goods_brief,"retail_price":goods.retail_price,"goods_number":goods.goods_number,"sell_volume":goods.sell_volume,"goods_unit":goods.goods_unit}
        data = {}
        data["gallery"] = galleryList
        data["goods"] = goodstotal
        data["goodsDetail"] = goodsDetail
        data["specificationList"] = specificationList
        return JsonResponse(data=data,safe=False)

def classification(request):

    data = {}
    goodsList = []
    goodsCount = GoodsInfo.objects.count()
    goodsAll = GoodsInfo.objects.all()
    goodsAllList = []
    for i in goodsAll:
        goodsAllList.append({"id":i.GoodsInfo_ID,"name":i.name,"goods_number":i.goods_number,"retail_price":i.retail_price,
                          "pic_url":i.pic_url,"goods_brief":i.goods_brief})

    for i in range(1, 6):
        res = GoodsInfo.objects.select_related().filter(category_ID=i)
        itemList = []
        for j in res:
            dict = {"id": j.GoodsInfo_ID, "name": j.name, "goods_number": j.goods_number,
                    "retail_price": j.retail_price, "pic_url": j.pic_url,"goods_brief":j.goods_brief}
            itemList.append(dict)
        goodsList.append(itemList)
    data["goodsCount"] = goodsCount
    data["goodsAllList"] = goodsAllList
    data["goodsList"] = goodsList
    return JsonResponse(data=data, safe=False)

def search(request):
    if request.method == "GET":
        keyWords = request.GET.get("keyword")
        sell_volume = GoodsInfo.objects.filter(name__contains=keyWords).order_by("-sell_volume")


        sell_volumeList = []
        for i in sell_volume:
            sell_volumeList.append({"id":i.GoodsInfo_ID,"name":i.name,"goods_number":i.goods_number,"retail_price":i.retail_price,
                          "pic_url":i.pic_url,"goods_brief":i.goods_brief})

        retail_price = GoodsInfo.objects.filter(name__contains=keyWords).order_by("retail_price")
        retail_priceList = []
        for i in retail_price:
            retail_priceList.append({"id":i.GoodsInfo_ID,"name":i.name,"goods_number":i.goods_number,"retail_price":i.retail_price,
                          "pic_url":i.pic_url,"goods_brief":i.goods_brief})

        Synthesis = GoodsInfo.objects.filter(name__contains=keyWords)
        SynthesisList = []
        for i in Synthesis:
            SynthesisList.append({"id":i.GoodsInfo_ID,"name":i.name,"goods_number":i.goods_number,"retail_price":i.retail_price,
                          "pic_url":i.pic_url,"goods_brief":i.goods_brief,'sell_volume':i.sell_volume})


        data = {}
        # data["sell_volumeList"] = sell_volumeList
        # data["retail_priceList"] = retail_priceList
        data["result"] = SynthesisList
        return JsonResponse(data=data)

